<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * Billing Service
 *
 */
class SDOServiceBilling extends ServiceBase
{
    # 正式环境Billing WSDL URL
    const BILLING_WSDL = 'BILLING_WSDL';
    # 测试环境Billing WSDL URL
    const PRE_BILLING_WSDL = 'PRE_BILLING_WSDL';
    const ADD_APP_EXISTS_CODE = - 10208011;
    const ADD_AREA_EXISTS_CODE = - 10208023;
    /**
     * 计费服务添加应用
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $name	  #应用名称
     * @param string $env     #环境（测试|正式）
     * @param int $timetypeid #计费类型
     * @param int $spId		  #服务商ID，默认为1，盛大
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function addApp (&$message, $appId, $name, $env = 'pre', $timetypeid = 5, $spId = 1, $notes = '')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['spId'] = $spId;
        $params['name'] = $name;
        $params['timetypeid'] = $timetypeid;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('addApp', $params, $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        if ($result->result !== "0") {
            $message = $result->message;
            if ($result->result == self::ADD_APP_EXISTS_CODE) {
                self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
                return true;
            }
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            return FALSE;
        }
        self::addArea($message, $appId, $env, 0, '0区');
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
    /**
     * 计费服务修改应用
     *
     * @param string $message #错误信息
     * @param int $appId  	  #APP ID	
     * @param string $name	  #应用名称
     * @param string $env     #环境（测试|正式）
     * @param int $timetypeid #计费类型
     * @param int $spId		  #服务商ID，默认为1，盛大
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function modifyApp (&$message, $appId, $name, $env = 'pre', $timetypeid = 5, $spId = 1, $notes = '')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['spId'] = $spId;
        $params['name'] = $name;
        $params['timetypeid'] = $timetypeid;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('modifyApp', $params, $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        if ($result->result !== "0") {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            $message = $result->message;
            return FALSE;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
    /**
     * 计费服务删除应用
     *
     * @param string $message #错误信息
     * @param int $appId  	  #APP ID	
     * @param string $name	  #应用名称
     * @param string $env     #环境（测试|正式）
     * @param int $timetypeid #计费类型
     * @param int $spId		  #服务商ID，默认为1，盛大
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function deleteApp (&$message, $appId, $name, $env = 'pre', $timetypeid = 5, $spId = 1, $notes = '')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        self::deleteArea($message, $appId, $env, 0, '0区');
        $params['serviceId'] = $appId;
        $params['spId'] = $spId;
        $params['name'] = $name;
        $params['timetypeid'] = $timetypeid;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('deleteApp', $params, $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        if ($result->result !== "0") {            
            $message = $result->message;
            if($result->result == -10208037){
                return TRUE;
            }
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            return FALSE;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
    /**
     * 配置区组信息
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $env     #环境（测试|正式）
     * @param int $areaId	  #区ID
     * @param string $name    #区名称
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function addArea (&$message, $appId, $env = 'pre', $areaId = 1, $name = '默认', $notes = '')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['areaId'] = $areaId;
        $params['name'] = $name;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('addArea', $params, $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        if ($result->result !== "0") {            
            $message = $result->message;
            if ($result->result == self::ADD_AREA_EXISTS_CODE) {
                return true;
            }
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            return FALSE;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
    /**
     * 修改区组信息
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $env     #环境（测试|正式）
     * @param int $areaId	  #区ID
     * @param string $name    #区名称
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function modifyArea (&$message, $appId, $env = 'pre', $areaId = 1, $name = '默认', $notes = '')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['areaId'] = $areaId;
        $params['name'] = $name;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if(isset($wsdl['options'])&&$wsdl['options']){
            $options = array_merge($options, $wsdl['options']);    
        }
        $result = self::soapRequest('modifyArea', $params, $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        if ($result->result !== "0") {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            $message = $result->message;
            return FALSE;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
    /**
     * 删除区组信息
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $env     #环境（测试|正式）
     * @param int $areaId	  #区ID
     * @param string $name    #区名称
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function deleteArea (&$message, $appId, $env = 'pre', $areaId = 1, $name = '默认', $notes = '')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['areaId'] = $areaId;
        $params['name'] = $name;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if(isset($wsdl['options'])&&$wsdl['options']){
            $options = array_merge($options, $wsdl['options']);    
        }
        $result = self::soapRequest('deleteArea', $params, $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\texception: ".$message, __CLASS__);
            return FALSE;
        }
        if ($result->result !== "0") {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
            $message = $result->message;
            return FALSE;
        }
        self::log("method: ".__FUNCTION__."\tparams: ".json_encode($params)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
        return true;
    }
    /**
     * 自检函数
     */
    public static function selfTest() 
    {
    	$sdk1 = sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL);
    	$sdk2 = sdk_fetch_config(SDK_ENV . self::BILLING_WSDL);
    	return self::curlTest(array($sdk1['wsdl'],$sdk2['wsdl']));
    }
}
